from django.shortcuts import render,HttpResponse,HttpResponseRedirect
# from django.http import HttpResponse
from . import models
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage

# Create your views here.
#首页
def index(request):
    categorys = models.Category.objects.all()
    banners = models.Banner.objects.filter(is_active=True)[0:5]
    tuis = models.Article.objects.filter(tui__id=1)[:4]
    new_articles = models.Article.objects.all().order_by('-id')[0:10]
    links = models.Link.objects.all()
    hots = models.Article.objects.all().order_by('views')[:10]
    remens = models.Article.objects.filter(tui__id=2)[:6]
    tags = models.Tag.objects.all()

    context = {
        'categorys':categorys,
        'banners':banners,
        'tuis':tuis,
        'new_articles':new_articles,
        'links':links,
        'hots':hots,
        'remens':remens,
        'tags':tags,
    }
    return render(request,'index.html',context)

#列表页
def list(request,lid):
    categorys = models.Category.objects.all()
    list = models.Article.objects.filter(category_id=lid)  # 获取通过URL传进来的lid，然后筛选出对应文章
    cname = models.Category.objects.get(id=lid)  # 获取当前文章的栏目名
    remen = models.Article.objects.filter(tui__id=2)[:6]  # 右侧的热门推荐
    allcategory = models.Category.objects.all()  # 导航所有分类
    tags = models.Tag.objects.all()  # 右侧所有文章标签
    page = request.GET.get('page')
    paginator = Paginator(list,5)
    try:
        list = paginator.page(page)
    except PageNotAnInteger:
        list =paginator.page(1)
    except EmptyPage:
        list = paginator.page(paginator.num_pages)
    return render(request, 'list.html', locals())

#内容页
def show(request,sid):
    categorys = models.Category.objects.all()
    comments = models.Comment.objects.filter(articleid=sid,is_active=True).order_by('-id')[0:20]
    show = models.Article.objects.get(id=sid)  # 查询指定ID的文章
    allcategory = models.Category.objects.all()  # 导航上的分类
    tags = models.Tag.objects.all()  # 右侧所有标签
    remen = models.Article.objects.filter(tui__id=2)[:6]  # 右侧热门推荐
    hot = models.Article.objects.all().order_by('?')[:10]  # 内容下面的您可能感兴趣的文章，随机推荐
    previous_blog = models.Article.objects.filter(created_time__gt=show.created_time, category=show.category.id).first()
    next_blog = models.Article.objects.filter(created_time__lt=show.created_time, category=show.category.id).last()
    show.views = show.views + 1
    show.save()
    return render(request, 'show.html', locals())

#标签页
def tag(request, tag):
    categorys = models.Category.objects.all()
    list = models.Article.objects.filter(tags__name=tag)  # 通过文章标签进行查询文章
    remen = models.Article.objects.filter(tui__id=2)[:6]
    allcategory = models.Category.objects.all()
    tname = models.Tag.objects.get(name=tag)  # 获取当前搜索的标签名
    page = request.GET.get('page')
    tags = models.Tag.objects.all()
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'tags.html', locals())

# 搜索页
def search(request):
    categorys = models.Category.objects.all()
    ss = request.GET.get('search')  # 获取搜索的关键词
    list = models.Article.objects.filter(title__icontains=ss)  # 获取到搜索关键词通过标题进行匹配
    remen = models.Article.objects.filter(tui__id=2)[:6]
    allcategory = models.Category.objects.all()
    page = request.GET.get('page')
    tags = models.Tag.objects.all()
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'search.html', locals())
# 关于我们

def about(request):
    remens = models.Article.objects.filter(tui__id=2)[:6]
    tags = models.Tag.objects.all()
    categorys = models.Category.objects.all()
    allcategory = models.Category.objects.all()
    return render(request, 'page.html', locals())


def comment(request,sid):
    content = request.GET.get('comment')
    sho = models.Article.objects.get(id=sid)
    new_comment= models.Comment.objects.create(content=content,articleid=sho)
    new_comment.save()
    return HttpResponseRedirect('show-%d.html' % sid)
